
package com.putao.album.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.putao.album.R;
import com.putao.album.child.ActivityQRcode;
import com.putao.album.main.bean.RequestSendInvitation;
import com.putao.album.share.ShareTools;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.HttpRequestUtil;
import com.putao.album.util.PuTaoConstants;
import com.putao.album.util.UmengTouchEvent;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

/**
 * Created by yanglun on 15/5/8.
 */
public class PtInvitePopupWindow implements View.OnClickListener {
    private Context mContext;
    private Activity mActivity;
    private static PopupWindow popupWindow = null;
    private static PtInvitePopupWindow inst;
    private PopupClickListener mPopupClickListener;
    public static ShareTools mShareTools;

    public static PopupWindow Create(Activity activity, PopupClickListener mPopupClickListener) {
        if (inst == null) {
            inst = new PtInvitePopupWindow();
            inst.mContext = activity;
            inst.mActivity = activity;
            inst.initPopuptWindow();
        }
        inst.mPopupClickListener = mPopupClickListener;
        return popupWindow;
    }

    protected void initPopuptWindow() {
        // TODO Auto-generated method stub

        View popupWindow_view = LayoutInflater.from(mContext).inflate(
                R.layout.layout_home_page_invite, null, false);
        RelativeLayout root_rl = (RelativeLayout) popupWindow_view.findViewById(R.id.root_rl);
        LinearLayout sms_invite_ll = (LinearLayout) popupWindow_view.findViewById(R.id.sms_invite_ll);
        LinearLayout wechat_invite_ll = (LinearLayout) popupWindow_view.findViewById(R.id.wechat_invite_ll);
        LinearLayout qbar_invite_ll = (LinearLayout) popupWindow_view.findViewById(R.id.qbar_invite_ll);
        LinearLayout qq_invite_ll = (LinearLayout) popupWindow_view.findViewById(R.id.qq_invite_ll);
        popupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        root_rl.setOnClickListener(this);
        sms_invite_ll.setOnClickListener(this);
        wechat_invite_ll.setOnClickListener(this);
        qbar_invite_ll.setOnClickListener(this);
        qq_invite_ll.setOnClickListener(this);
        mShareTools = new ShareTools(mActivity);
        root_rl.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    popupWindow.dismiss();
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        InvitationType type = null;
        switch (v.getId()) {
            case R.id.sms_invite_ll:
                type = InvitationType.SMS;
                MobclickAgent.onEvent(mContext, UmengTouchEvent.INVITE_MESSAGE);
                break;
            case R.id.wechat_invite_ll:
                type = InvitationType.WEIXIN;
                MobclickAgent.onEvent(mContext, UmengTouchEvent.INVITE_WECHAT);
                break;
            case R.id.root_rl:
                popupWindow.dismiss();
                break;
            case R.id.qbar_invite_ll:
                type = InvitationType.QRCODE;
                break;
            case R.id.qq_invite_ll:
                type = InvitationType.QQ;
                break;
        }
        if (mPopupClickListener != null && type != null) {
            querySendInvitation(type, mPopupClickListener.getRelateionShip());
        }
    }

    public enum InvitationType {
        SMS(1),
        WEIXIN(2),
        QRCODE(3),
        QQ(4);

        private int value;

        InvitationType(int value) {
            this.value = value;
        }

        int getValue() {
            return this.value;
        }
    }

    void doInvite(InvitationType type, String content) {
        switch (type) {
            case SMS:
                ActivityHelper.startSMSActivity(mActivity, content);
                break;
            case QQ:
                mShareTools.doInviteByQQ(content);
                break;
            case QRCODE:
                Bundle bundle = new Bundle();
                bundle.putString("relation", mPopupClickListener.getRelateionShip());
                ActivityHelper.startActivity(mActivity, ActivityQRcode.class, bundle);
                break;
            case WEIXIN:
                mShareTools.doInviteByWeiXin(content);
                break;
        }
        if (popupWindow != null)
            popupWindow.dismiss();
    }

    private void querySendInvitation(final InvitationType type, String relation) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", CommonUtils.getUsrId());
        params.put("relationship", relation);
        params.put("baby_id", CommonUtils.getBabyId());
        params.put("type", String.valueOf(type.getValue()));
        HttpRequestUtil.doHttpPostRequest(mActivity, PuTaoConstants.API_SEND_INVITATION,
                new RequestParams(HttpRequestUtil.buildHttpRequestParams(params)), new HttpRequestUtil.RequestResponseListener() {
                    @Override
                    public void success(int statuscode, String response) {
                        Gson gson = new Gson();
                        RequestSendInvitation requestInfo = gson.fromJson(response, RequestSendInvitation.class);
                        if (requestInfo != null) {
                            String m_invite_send_msg = requestInfo.send_msg;
                            doInvite(type, m_invite_send_msg);
                        }
                    }

                    @Override
                    public void failed(int statuscode, String response) {

                    }
                });
    }

    public interface PopupClickListener {
        String getRelateionShip();
    }
}